package com.example.fragmentest.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.fragmentest.Beam.User;
import com.example.fragmentest.LoginActivity;
import com.example.fragmentest.R;

import org.litepal.LitePal;

public class ThirdFragment extends Fragment {
    private EditText oldpsdedit,newpsdedit;
    private Button changepsdbtn;
    public ThirdFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_third, container, false);
        initView(view);
        setListener();
        return view;
    }

    private void setListener() {
        changepsdbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String psd_old=oldpsdedit.getText().toString();
                String psd_new=newpsdedit.getText().toString();
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
                int uid=sp.getInt("userid",-1);
                String password=sp.getString("password","");
                if (psd_old.equals(password)){
                    User user=LitePal.find(User.class,uid);
                    user.setPsd(psd_new);
                    user.save();
                    showToast("修改密码成功");
                    Intent intent=new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                    getActivity().finish();
                }else {
                    showToast("原密码输入错误");

                }
            }
        });
    }
    private void showToast(String str){
        Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();

    }

    private void initView(View view) {
        oldpsdedit=view.findViewById(R.id.oldpsdedit);
        newpsdedit=view.findViewById(R.id.newpsdedit);
        changepsdbtn=view.findViewById(R.id.changepsdbtn);
    }

}
